<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

require_once(t3lib_extMgm::extPath('auxo') . 'core/class.tx_auxo_Cache.php');

/**
 * @package auxo
 * @subpackage tests
 * @version $Id$
 */

/**	
 * Cache Testsuite 
 *	
 * @package auxo
 * @subpackage tests	
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class  tx_auxo_Cache_testcase extends PHPUnit_Framework_testcase  {
	/**
	 * A Cache
	 *
	 * @var tx_auxo_Cache
	 */
	private $cache = NULL;
	
	/**
	 * Sets up a test enviroment of this testcase.
	 * 
	 * @return void
	 */
	public function setUp() {
		$this->cache = new tx_auxo_Cache('TEST');
		$this->cache->clean();
	}
	
	/**
	 * Testcase: tests type of cache
	 * 
	 * @return void
	 */
	public function testCacheAvailable() {
		$this->assertTrue($this->cache instanceof tx_auxo_Cache);
	}

	/**
	 * Testcase: tests if an added object is in cache
	 * 
	 * @return void
	 */
	public function testCacheAddHas() {
		$this->cache->add('0001', '0001_Test_Object');		
		$this->assertTrue($this->cache->has('0001'));
	}

	/**
	 * Testcase: tests if an added object is in cache and could be retrieved
	 * 
	 * @return void
	 */
	public function testCacheAddGet() {
		$this->cache->add('0002', '0002_Test_Object');		
		$this->assertSame($this->cache->get('0002'), '0002_Test_Object');
	}	

	/**
	 * Testcase: Removes an object from cache
	 * 
	 * @return void
	 */	
	public function testRemoveFromCache() {
		$this->cache->add('abc', 'abcdefghijklmnoqrstuvwxyz1234567890');		
		$this->assertTrue($this->cache->has('abc'));
		$this->cache->remove('abc');
		$this->assertFalse($this->cache->has('abc'));		
	}

	/**
	 * Testcase: add and get objects to cache with limited memory size
	 * so that objects have to be stored in a local file
	 * 
	 * @return void
	 */
	public function testFileCache() {
		$this->cache->setMemorySize(2);
		$this->cache->setMemoryRequired(1);
		$this->cache->add('abc', 'abc String');		
		$this->cache->add('123', '123 String');		
		$this->cache->add('ABC', 'ABC String');		
		$this->assertTrue($this->cache->has('abc'));
		$this->assertTrue($this->cache->has('123'));
		$this->assertTrue($this->cache->has('ABC'));
		$this->assertEquals($this->cache->get('abc'), 'abc String');		
		$this->assertEquals($this->cache->get('123'), '123 String');		
		$this->assertEquals($this->cache->get('ABC'), 'ABC String');		
	}
	
	/**
	 * Testcase: add and get objects to a cache with limited memory size
	 * so that objects have to be stored in a local file after cleaning
	 * those objects should be no longer available.
	 * 
	 * @return void
	 */
	public function testCleanCache() {
		$this->cache->setMemorySize(2);
		$this->cache->setMemoryRequired(1);
		$this->cache->add('abc', 'abc String');		
		$this->cache->add('123', '123 String');		
		$this->cache->add('ABC', 'ABC String');		
		$this->cache->clean();
		$this->assertFalse($this->cache->has('abc'));
		$this->assertFalse($this->cache->has('123'));
		$this->assertFalse($this->cache->has('ABC'));
	}
	
	/**
	 * Tears down a created test enviroment of this testcase.
	 */
	public function tearDown() {
		unset($this->cache);
	}
}